﻿/// <reference path="jquery 1.5.2 vsdoc.js" />


function calendar(labID, year, month) {
    $.ajax({
        type: "POST",
        url: "/Ajax/Service.ashx?category=GetVisitRequest",
        data: "Language=" + Language + "&LabID=" + labID + "&Year=" + year + "&Month=" + month,
        beforeSend: function () {
            if (!labID || !year || !month) {
                return false;
            }
        },
        success: function (data) {
            $("#calendar").html("");
            $('#calendar').fullCalendar({ editable: false, year: year, month: (month - 1) });
            $(".fc-header-right").html("");
            $(".fc-day-content").find("div:first").remove();
            $(".fc-dayclose").each(function () {
                $(this).removeClass("fc-dayclose");
            });
            $('td.fc-widget-content').each(function (datas, index) {
                if ($(this).hasClass('fc-other-month')) {
                    $(this).find(".fc-day-number").html("");
                    return;
                }
                $('.fc-state-highlight').css('background-color', 'white');
                var day = $(this).find('div.fc-day-number');
                init(data, day.text(), $(this));
            });

        }
    });
};


function init(data, date, target) {
    //not supported in IE6
    //var msg = JSON.parse(data);
    var msg = eval("(" + data + ")");

    for (var i = 0; i < msg.length; i++) {
        var day = msg[i];
        if (day.Date == date) {
            //console.log(day.Date);
            var orders = day.Orders;
            if (day.LabStatus == "Closed") {
                target.addClass("fc-dayclose");
                target.find(".fc-day-content").append("<div  class='fc-event-inner fc-event-skin' style='text-align:center;white-space:normal;' >" + day.CloseReason + "</div>");
                continue;
            }

            for (var j = 0; j < orders.length; j++) {
                var divToAttend = target.find('.fc-day-content');
                var id = date + "_" + (Math.random() * 100000000000000000)
                if (orders[j].Status == "Approved") {

                    divToAttend.append("<div id=" + id + " class='fc-event-inner fc-event-skin' style='text-align:center;color:green;'>"
                    + "<a style='color:green;' href=" + orders[j].LinkURL + ">" + orders[j].Time + "</a></div>");
                    addPopUp($("#" + id), orders[j].RequestUserDetail);


                }
                if (orders[j].Status == "Completed") {
                    divToAttend.append("<div id=" + id + " class='fc-event-inner fc-event-skin' style='text-align:center;color:orange;'>"
                    + "<a  style='color:orange;' href=" + orders[j].LinkURL + " >" + orders[j].Time + "</a></div>");
                    addPopUp($("#" + id), orders[j].RequestUserDetail);

                }
                if (orders[j].Status == "Canceled") {
                    divToAttend.append("<div id=" + id + " class='fc-event-inner fc-event-skin' style='text-align:center;color:gray;'>"
                    + "<a style='color:gray;' href=" + orders[j].LinkURL + ">" + orders[j].Time + "</a></div>");
                    addPopUp($("#" + id), orders[j].RequestUserDetail);

                }

                //                if (orders[j].Status == "Pending") {
                //                    divToAttend.append("<div id=" + id + " class='fc-event-inner fc-event-skin' style='text-align:center;color:blue;height:16px;'>"
                //                    + "<a style='color:blue;' href=" + orders[j].LinkURL + ">"+ orders[j].Time + "</a></div>");
                //                    addPopUp($("#" + id), orders[j].RequestUserDetail);
                //                }

            }
        }

    }
};
function addPopUp(divToAttend, RequestUserDetail) {
    if (RequestUserDetail.RequestType == "Visit" || RequestUserDetail.RequestType == "访问") {
        divToAttend.attr("title", RequestUserDetail.RequestType + "\r\n" + RequestUserDetail.CompanyName
        + "\r\n" + RequestUserDetail.Dept + "\r\n" + RequestUserDetail.UserName +
        "\r\n" + RequestUserDetail.Telephone);

    }
    else {
        divToAttend.attr("title", RequestUserDetail.RequestType
        + "\r\n" + RequestUserDetail.Dept + " \r\n" + RequestUserDetail.UserName +
        " \r\n" + RequestUserDetail.Telephone);
    }

};


function getLabAndDate() {

    var lab = $("#" + labId + " option:selected").attr("value");
    var year = $("#" + yearId + " option:selected").attr("value");
    var month = $("#" + monthId + " option:selected").attr("value");
    calendar(lab, year, month);

};

function getLabList() {
    var LocationId = $("#" + locationId + " option:selected").attr("value");

    $.ajax({
        type: "POST",
        url: "/Ajax/Service.ashx?category=GetLabList",
        data: "LocationId=" + LocationId,
        beforeSend: function () {
            if (!LocationId) {
                return false;
            }
        },
        success: function (datas) {

            var msg = eval("(" + datas + ")");
            $("#" + labId).html("");
            $.each(msg, function (index, data) {
                $("#" + labId).append("<option value='" + data.LabId + "'>" + data.Name + "</option>");
            });
        }
    });
}
